﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Web;
using BaseData;
using BaseData.Exts;
using BaseData.HelperEzui;
using BaseData.Models;
using BaseLib.Helper;
using BaseUtility;

namespace WebUI.Ashx.Acc.Staff
{
    /// <summary>
    /// NewsAdminNodeFormSubmit 的摘要说明
    /// </summary>
    public class NewsAdminNodeFormSubmit : Base, IHttpHandler
    {
        private static readonly AccClass _accId = new AccExts(new GetAccType().Parse(Request.QueryString["url"])).Instances;
        public void ProcessRequest(HttpContext context)
        {
            if (_accId.Id == 0 || string.IsNullOrWhiteSpace(_accId.Name) || _accId.Type == 0) return;
            context.Response.Write(this.Get(context).ObjectsToJson());
            context.Response.ContentType = "text/plain";
        }
        MsgErr Get(HttpContext context)
        {
            var nodeId = context.Request.QueryString["id"].ToInt(0);
            if (nodeId == 0) return new MsgErr() { message = new StringBuilder("错误：至少选中一个节点。"), };
            var flag = 0;
            var action = (Enums.Action)Enum.Parse(typeof(Enums.Action), context.Request.QueryString["action"], true);
            if (flag != 0) return new MsgErr() { message = new StringBuilder("指定的上级节点ID不存在！") };
            var tempInit = new Web_Base_News_Node
            {
            };
            using (var ctx = new ContextExt())
            {
                var nodes = from q in ctx.Web_Base_News_Node select q;
                if (nodes.Any() && !nodes.Any(w => w.PkId == nodeId)) return new MsgErr() { message = new StringBuilder("指定的上级节点ID不存在！") };
                try
                {
                    Web_Base_News_Node nodeInit;
                    switch (action)
                    {
                        case Enums.Action.ActNew:
                            nodeInit = new Web_Base_News_Node();
                            nodeInit = this.InitNode(nodeInit, nodeId, action);
                            ctx.Web_Base_News_Node.AddOrUpdate(m => m.PkId, nodeInit);
                            break;
                        case Enums.Action.ActMove:
                            // nodeInit.ParentId = nodeId;
                            nodeInit = new Web_Base_News_Node();
                            break;
                        default:
                            nodeInit = nodes.FirstOrDefault(w => w.PkId == nodeId);
                            nodeInit = this.InitNode(nodeInit, nodeId, action);
                            break;
                    }
                    ctx.SaveChanges();
                    return new MsgErr() { error = 0, id = nodeInit.PkId, attributes = new Attributes { key = nodeInit.PkId, value = nodeInit.PkId, }, message = new StringBuilder("节点操作成功了！") };
                }
                catch (Exception err)
                {
                    return new MsgErr() { message = new StringBuilder("错误：" + err.Message), };
                }
            }
        }

        private Web_Base_News_Node InitNode(Web_Base_News_Node node, int nodeId, Enums.Action action)
        {
            node.ParentId = nodeId;
            node.Title = Request.Form["Title"].ToNull();
            node.TitleEn = Request.Form["TitleEn"].ToNull();
            node.Content = Request.Form["Content"].ToNull();
            node.CssName = Request.Form["CssName"].ToNull();
            node.Folder = Request.Form["Folder"].ToNull();
            node.Url = Request.Form["Url"].ToNull();
            node.FlagStatus = Request.Form["FlagStatus"].ToInt();
            node.FlagPass = Request.Form["FlagPass"].ToInt();
            node.FlagStop = Request.Form["FlagStop"].ToInt();
            node.FlagView = Request.Form["FlagView"].ToInt();
            // flagMenu = context.Request.Form["flagMenu"].ToInt();
            // KeyInt1 = context.Request.Form["KeyInt1"].ToNull();
            // KeyInt2 = context.Request.Form["KeyInt2"].ToNull();
            // KeyInt3 = context.Request.Form["KeyInt3"].ToNull();
            // KeyFloat1 = context.Request.Form["KeyFloat1"].ToNull();
            // KeyFloat2 = context.Request.Form["KeyFloat2"].ToNull();
            // KeyFloat3 = context.Request.Form["KeyFloat3"].ToNull();
            // KeyString1 = context.Request.Form["KeyString1"].ToNull();
            // KeyString2 = context.Request.Form["KeyString2"].ToNull();
            // KeyString3 = context.Request.Form["KeyString3"].ToNull();
            // KeyDateTime1 = context.Request.Form["KeyDateTime1"].ToNull();
            // KeyDateTime2 = context.Request.Form["KeyDateTime2"].ToNull();
            // KeyDateTime3 = context.Request.Form["KeyDateTime3"].ToNull();
            node.TemplateIndex = Request.Form["TemplateIndex"].ToNull();
            node.TemplateNode = Request.Form["TemplateNode"].ToNull();
            node.TemplateList = Request.Form["TemplateList"].ToNull();
            node.TemplateView = Request.Form["TemplateView"].ToNull();
            // Banner = context.Request.Form["Banner"].ToNull();
            // Guide = context.Request.Form["Guide"].ToNull();
            // Icon = context.Request.Form["Icon"].ToNull();
            return node;
        }

        private Web_Base_News_Node_Ext InitNodeExt(Web_Base_News_Node_Ext node, int nodeId, Enums.Action action)
        {
            switch (action)
            {
                case Enums.Action.ActNew:
                    break;
                case Enums.Action.ActEdit:
                    break;
                case Enums.Action.ActDelete:
                    break;
                case Enums.Action.ActErase:
                    break;
            }
            node.NodeId = nodeId;
            node.PassId = 0;
            node.UpdateId = 0;
            node.CreateId = 0;
            node.EditorId = 0;
            node.KeyDateTime1 = Request.Form["KeyDateTime1"].ToNull();
            node.KeyDateTime2 = Request.Form["KeyDateTime2"].ToNull();
            node.KeyDateTime3 = Request.Form["KeyDateTime3"].ToNull();
            node.KeyFloat1 = Request.Form["KeyFloat1"].ToNull();
            node.KeyFloat2 = Request.Form["KeyFloat2"].ToNull();
            node.KeyFloat3 = Request.Form["KeyFloat3"].ToNull();
            node.KeyInt1 = Request.Form["KeyInt1"].ToNull();
            node.KeyInt2 = Request.Form["KeyInt2"].ToNull();
            node.KeyInt3 = Request.Form["KeyInt3"].ToNull();
            node.KeyString1 = Request.Form["KeyString1"].ToNull();
            node.KeyString2 = Request.Form["KeyString2"].ToNull();
            node.KeyString3 = Request.Form["KeyString3"].ToNull();
            node.KeyString4 = Request.Form["KeyString4"].ToNull();
            node.KeyString5 = Request.Form["KeyString5"].ToNull();
            node.KeyString6 = Request.Form["KeyString6"].ToNull();
            node.Memo = Request.Form["Memo"].ToNull();
            node.Message = Request.Form["Message"].ToNull();
            node.Remark = Request.Form["Remark"].ToNull();
            return node;
        }

        private Web_Base_Exts_Resx InitNodeResx(Web_Base_Exts_Resx node, int nodeId, Enums.Action action)
        {
            node.NodeId = nodeId;
            node.InfoId = 0;
            node.Banner = Request.Form["Banner"].ToNull();
            node.BannersCount = 0;
            node.Doc = Request.Form["Doc"].ToNull();
            node.DocsCount = 0;
            node.Guide = Request.Form["Guide"].ToNull();
            node.GuidesCount = 0;
            node.Icon = Request.Form["Icon"].ToNull();
            node.IconsCount = 0;
            node.Image = Request.Form["Image"].ToNull();
            node.ImagesCount = 0;
            node.Media = Request.Form["Media"].ToNull();
            node.MediaCount = 0;
            node.Zip = Request.Form["Zip"].ToNull();
            node.ZipCount = 0;
            return node;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}